package javax.realtime;

public class NativeHelper {
	static
	{
		System.loadLibrary("nativehelper");
		if(!sched_setscheduler_FIFO(0)) throw new SecurityException();
	}
	
	private static native int getMinRTPriority_FIFO();
	private static native int getMaxRTPriority_FIFO();
	private static native int nanosleep0(long millis, long nanos);	
	private static native boolean sched_setscheduler_FIFO(int pid);
	
	private static int minRTPriority_FIFO = getMinRTPriority_FIFO();
	private static int maxRTPriority_FIFO = getMaxRTPriority_FIFO();
	
	public static native boolean sched_setaffinity(int pid, int cpu);
	public static native int setpriority(int tid, int prio);
	public static native int gettid();
	
	/**
	 * Priority mapping rule:
	 *  min: minimum RTVM task
	 *  norm: ... 
	 *  max-3: ...
	 *  max-2: maximum RTVM task
	 *  
	 *  max-1: VM - GC
	 *  max: timer thread
	 */
	public static int getRunningRTPriority()
	{
		return 0;
	}
	public static int getMinRTPriority()
	{
		return minRTPriority_FIFO;
	}
	public static int getMaxRTPriority()
	{
		return maxRTPriority_FIFO - 2;
	}
	public static int getMaxTimerPriority()
	{
		return maxRTPriority_FIFO;
	}
	/*
	public static int getNormRTPriority()
	{
		return (getMaxRTPriority() - getMinRTPriority()) / 2 + getMinRTPriority();
	}
	*/
	public static void nanosleep(long millis, long nanos)
	{
		long sleepTimeRemain = millis * 1000000 + nanos;
		long startTime;
		nanosleep0(millis, nanos);
		/*
		do
		{
			startTime= System.nanoTime();
			nanosleep0(millis, nanos);
			sleepTimeRemain -= (System.nanoTime() - startTime);
			millis = sleepTimeRemain / 1000000;
			nanos = sleepTimeRemain % 1000000;
		}while(sleepTimeRemain > 0);*/
	}
}
